home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / EMBL Search / Sources / environment.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-04  |  8.8 KB  |  400 lines  |  [TEXT/KAHL]

  1. /*
  2. *********************************************************************
  3. *    
  4. *    environment.c
  5. *    Checks operating system environment
  6. *        
  7. *    Rainer Fuchs
  8. *    EMBL Data Library
  9. *    Postfach 10.2209
  10. *    D-6900 Heidelberg, FRG
  11. *    E-mail: fuchs@embl-heidelberg.de
  12. *
  13. *    Copyright © 1992 EMBL Data Library
  14. *        
  15. **********************************************************************
  16. *    
  17. */ 
  18.  
  19. #include <Traps.h>
  20. #include <GestaltEqu.h>
  21. #include <Folders.h>
  22.  
  23. #include "EMBL-Search.h"
  24. #include "EMBL-Search.rsrc.h"
  25.  
  26. /*
  27. ******************************* Prototypes ***************************
  28. */
  29.  
  30. #include "environment.h"
  31. #include "util.h"
  32.  
  33. static Boolean IsGestaltAvail(void);
  34. static short GetSystemVersion(void);
  35. static Boolean IsTempMemAvail(void);
  36. static Boolean IsColorQDAvail(void);
  37. static Boolean IsBalloonHelpAvail(void);
  38. static Boolean IsAppleEventsAvail(void);
  39. static Boolean IsFindFolderAvail(void);
  40. static Boolean IsAliasMgrAvail(void);
  41. static Boolean IsFSSpecAvail(void);
  42. static short FindSysFolder(void);
  43. static short FindTempFolder(void);
  44. static short FindPrefFolder(void);
  45. static Boolean TrapAvailable(short theTrap);
  46. static TrapType GetTrapType(short theTrap);
  47. static short NumToolboxTraps(void);
  48. static long GetGestaltResult(OSType gestaltSelector);
  49.  
  50.  
  51. /*
  52. ******************************** Global variables *****************
  53. */
  54.  
  55. short        gSysVRefNum;
  56. short        gTempVRefNum;
  57. short        gPrefVRefNum;
  58. short        gSysVersion;
  59.  
  60. Boolean    gHasGestalt;
  61. Boolean    gHasAppleEvents;
  62. Boolean    gHasTempMemory;
  63. Boolean    gHasFindFolder;
  64. Boolean    gHasBalloonHelp;
  65. Boolean    gHasAliasMgr;
  66. Boolean    gHasFSSpec;
  67. Boolean    gHasColorQD;
  68.  
  69. Str255    gAppName;
  70. short        gAppResRef;                            /* Application rsrc file ref num */
  71.  
  72. /**************************************
  73. *    Check system version, availability of certain functions, etc.
  74. */
  75.  
  76. void CheckEnvironment()
  77. {
  78.     Handle    apParam;
  79.     
  80.     /* Can we use Gestalt? */
  81.     gHasGestalt=IsGestaltAvail();
  82.  
  83.     /* No support for System versions less than 6.0.x */
  84.     if ( (gSysVersion=GetSystemVersion()) < 0x0605 )
  85.         FatalErrorMsg(ERR_VERSION);
  86.  
  87.     /* Get application name */
  88.     GetAppParms(gAppName, &gAppResRef, &apParam);
  89.  
  90.     /* Check for availability of some specific functions */
  91.     gHasTempMemory        = IsTempMemAvail();
  92.     gHasBalloonHelp    = IsBalloonHelpAvail();
  93.     gHasFindFolder        = IsFindFolderAvail();
  94.     gHasAppleEvents    = IsAppleEventsAvail();
  95.     gHasAliasMgr        = IsAliasMgrAvail();
  96.     gHasFSSpec            = IsFSSpecAvail();
  97.     gHasColorQD            = IsColorQDAvail();
  98.     
  99.     /* Get system folder vRefNum */
  100.     gSysVRefNum = FindSysFolder(); /* error check ... */
  101.     
  102.     /* Find temporary items folder
  103.         If FindFolder is available we use/create the "Temporary Items" folder,
  104.         otherwise we simply use the System folder (we don't look for a
  105.         subfolder in this case, because names are language-dependent
  106.     */
  107.     if( (gTempVRefNum = FindTempFolder()) == 0)
  108.         gTempVRefNum = gSysVRefNum;
  109.         
  110.     /* Find preferences folder
  111.         If FindFolder is available we use/create the "Preferences" folder,
  112.         otherwise we simply use the System folder (we dont look for a
  113.         subfolder in this case, because names are language-dependent
  114.     */
  115.     if( (gPrefVRefNum = FindPrefFolder()) == 0)
  116.         gPrefVRefNum = gSysVRefNum;
  117. }
  118.  
  119. /**************************************
  120. *    Check for availability of Gestalt
  121. *    Return value:    TRUE, if Gestalt available
  122. *                        FALSE, if not
  123. */
  124.  
  125. static Boolean IsGestaltAvail()
  126. {
  127. #define _Gestalt 0xA1Ad
  128.  
  129.     return(TrapAvailable(_Gestalt));
  130. }
  131.  
  132. /**************************************
  133. *    Get System version
  134. *    Return value:    System version, or 0 if not available
  135. */
  136.  
  137. static short GetSystemVersion()
  138. {
  139.     SysEnvRec theWorld;
  140.  
  141.     if(gHasGestalt)
  142.         return( (short)GetGestaltResult (gestaltSystemVersion) );
  143.     else
  144.         return( (SysEnvirons(curSysEnvVers,&theWorld)) ? theWorld.systemVersion : 0);
  145. }
  146.  
  147. /**************************************
  148. *    Check for availability of temporary memory calls
  149. *    Return value:    TRUE, if available
  150. *                        FALSE, if not
  151. */
  152.  
  153. static Boolean IsTempMemAvail()
  154. {
  155.     long result;
  156.     
  157.     result=GetGestaltResult(gestaltOSAttr);
  158.     if(result && (result & (1 << gestaltTempMemSupport)))
  159.         return(TRUE);
  160.     else return(FALSE);
  161.  
  162. }
  163.  
  164. /**************************************
  165. *    Check for availability of color QuickDraw
  166. *    Return value:    TRUE, if available
  167. *                        FALSE, if not
  168. */
  169.  
  170. static Boolean IsColorQDAvail()
  171. {
  172.     long result;
  173.     SysEnvRec theWorld;
  174.     
  175.     if(gHasGestalt) {
  176.         result = GetGestaltResult(gestaltQuickdrawVersion);
  177.         if(result && result >= 0x100)
  178.             return(TRUE);
  179.         else return(FALSE);
  180.     }
  181.     else
  182.         return( (SysEnvirons(curSysEnvVers,&theWorld)) ? theWorld.hasColorQD : FALSE);
  183. }
  184.  
  185. /**************************************
  186. *    Check for Help Manager
  187. *    Return value:    TRUE, if available
  188. *                        FALSE, if not
  189. */
  190.  
  191. static Boolean IsBalloonHelpAvail()
  192. {
  193.     long result;
  194.     
  195.     result=GetGestaltResult(gestaltHelpMgrAttr);
  196.     if(result && (result & (1 << gestaltHelpMgrPresent)))
  197.         return(TRUE);
  198.     else return(FALSE);
  199.  
  200. }
  201.  
  202. /**************************************
  203. *    Check for AppleEvents
  204. *    Return value:    TRUE, if available
  205. *                        FALSE, if not
  206. */
  207.  
  208. static Boolean IsAppleEventsAvail()
  209. {
  210.     long result;
  211.     
  212.     result=GetGestaltResult(gestaltAppleEventsAttr);
  213.     if(result && (result & (1 << gestaltAppleEventsPresent)))
  214.         return(TRUE);
  215.     else return(FALSE);
  216.  
  217. }
  218.  
  219. /**************************************
  220. *    Check for FindFolder
  221. *    Return value:    TRUE, if available
  222. *                        FALSE, if not
  223. */
  224.  
  225. static Boolean IsFindFolderAvail()
  226. {
  227.     long result;
  228.     
  229.     result=GetGestaltResult(gestaltFindFolderAttr);
  230.     if(result && (result & (1 << gestaltFindFolderPresent)))
  231.         return(TRUE);
  232.     else return(FALSE);
  233.  
  234. }
  235.  
  236. /**************************************
  237. *    Check for Alias Manager
  238. *    Return value:    TRUE, if available
  239. *                        FALSE, if not
  240. */
  241.  
  242. static Boolean IsAliasMgrAvail()
  243. {
  244.     long result;
  245.     
  246.     result=GetGestaltResult(gestaltAliasMgrAttr);
  247.     if(result && (result & (1 << gestaltAliasMgrPresent)))
  248.         return(TRUE);
  249.     else return(FALSE);
  250.  
  251. }
  252.  
  253. /**************************************
  254. *    Check for System 7 File Manager calls
  255. *    Return value:    TRUE, if available
  256. *                        FALSE, if not
  257. */
  258.  
  259. static Boolean IsFSSpecAvail()
  260. {
  261.     long result;
  262.     
  263.     result=GetGestaltResult(gestaltFSAttr);
  264.     if(result && (result & (1 << gestaltHasFSSpecCalls)))
  265.         return(TRUE);
  266.     else return(FALSE);
  267.  
  268. }
  269.  
  270. /**************************************
  271. *    Obtain working directory refnum for system folder
  272. *    Return value:    working directory refnum or 0, if not available
  273. */
  274.  
  275. static short FindSysFolder()
  276. {
  277.     SysEnvRec    envRec;
  278.     
  279.     if (!SysEnvirons (curSysEnvVers, &envRec))
  280.         return(envRec.sysVRefNum);
  281.     else return(0);
  282. }
  283.  
  284. /**************************************
  285. *    Obtain working directory refnum for "Temporary Items" folder.
  286. *    Return value:    working directory refnum or 0, if not available
  287. */
  288.  
  289. static short FindTempFolder()
  290. {
  291.     short foundVRefNum;
  292.     long foundDirID;
  293.     OSErr err;
  294.     short    wdRefNum;
  295.         
  296.     err=noErr;
  297.     
  298.     /* find folder and create it if necessary */
  299.     if(gHasFindFolder)
  300.         err=FindFolder(kOnSystemDisk, kTemporaryFolderType, kCreateFolder, 
  301.                 &foundVRefNum, &foundDirID);
  302.                 
  303.     if(!gHasFindFolder || err != noErr)
  304.         return(0);
  305.     else    /* convert directory ID to working directory refnum */
  306.         err=OpenWD(foundVRefNum,foundDirID,kApplSignature,&wdRefNum);
  307.  
  308.     return( (err != noErr) ? 0 : wdRefNum);
  309. }
  310.  
  311.  
  312. /**************************************
  313. *    Get working directory refnum for "Preferences" folder.
  314. *    Return value:    working directory refnum or 0, if not available
  315. */
  316.  
  317. static short FindPrefFolder()
  318. {
  319.     short foundVRefNum;
  320.     long foundDirID;
  321.     OSErr err;
  322.     short wdRefNum;
  323.     
  324.     err=noErr;
  325.     
  326.     /* find folder and create it if necessary */
  327.     if(gHasFindFolder)
  328.     err=FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, 
  329.                 &foundVRefNum, &foundDirID);
  330.                 
  331.     if(!gHasFindFolder || err != noErr)
  332.         return(0);
  333.     else    /* convert directory ID to working directory refnum */
  334.         err = OpenWD(foundVRefNum,foundDirID,kApplSignature,&wdRefNum);
  335.  
  336.     return( (err != noErr) ? 0 : wdRefNum);
  337. }
  338.  
  339. /**************************************
  340. *    Check availability of trap
  341. *    Return value:    TRUE, if available
  342. *                        FALSE, if not
  343. */
  344.  
  345. static Boolean TrapAvailable(short theTrap)
  346. {
  347.     TrapType tType;
  348.     
  349.     tType=GetTrapType(theTrap);
  350.     if(tType == ToolTrap) {
  351.         theTrap &= 0x07FF;
  352.         if(theTrap >= NumToolboxTraps())
  353.             theTrap = _Unimplemented;
  354.     }
  355.     return(     NGetTrapAddress(theTrap,tType) !=
  356.                 NGetTrapAddress(_Unimplemented,ToolTrap) );
  357. }
  358.  
  359. /**************************************
  360. *    Obtain trap type
  361. *    Return value:    trap type
  362. */
  363.  
  364. static TrapType GetTrapType(short theTrap)
  365. {
  366. #define TrapMask 0x0800
  367.  
  368.     return( (theTrap & TrapMask > 0) ? ToolTrap : OSTrap);
  369. }
  370.  
  371. /**************************************
  372. *    Check size of trap table
  373. *    Return value:    size of trap table
  374. */
  375.         
  376. static short NumToolboxTraps()
  377. {
  378.     return( (NGetTrapAddress(_InitGraf, ToolTrap) ==
  379.                 NGetTrapAddress(0xAA6E,ToolTrap) ) ? 0x200 : 0x400);
  380. }
  381.  
  382. /**************************************
  383. *    Call Gestalt to get system information.
  384. *    Return value:    value returned by Gestalt, or 0 if Gestalt is not available
  385. */
  386.  
  387. static long    GetGestaltResult(OSType gestaltSelector)
  388. {
  389.     long    gestaltResult;
  390.     
  391.     if(!gHasGestalt)
  392.         return(0L);
  393.         
  394.     if (Gestalt(gestaltSelector, &gestaltResult) == noErr)
  395.         return(gestaltResult);
  396.     else
  397.         return(0);
  398. }
  399.  
  400.